package org.openkoala.koala.popup.preference;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.config.DbConfigUtil;
import org.openkoala.koala.config.vo.DBConfigVO;

/**
 * 数据库配置面板内容
 * @author xmfang
 *
 */
public class DataBaseConfig {

	//数据库配置信息
	private Combo dataBaseType;
	private Combo showSql;
	private Text dialect;
	private Text jdbcDriver;
	private Text connectionURL;
	private Combo hbm2ddl;
	private Text username;
	private Text password;
	
	//数据库配置信息
	private DBConfigVO projectDbConfigVO;
	private DBConfigVO selectedDbConfigVO;
	
	private Map<String, DBConfigVO> dbConfigVosMap = new HashMap<String, DBConfigVO>();

	public DataBaseConfig(DBConfigVO projectDbConfigVO) {
		if (projectDbConfigVO == null) {
			this.projectDbConfigVO = new DBConfigVO();
		} else {
			this.projectDbConfigVO = projectDbConfigVO;
		}
		
		initDbConfigVosMap();
	}
	
	private void initDbConfigVosMap() {
		try {
			for (DBConfigVO dbConfigVO : DbConfigUtil.getDBConfig()) {
				dbConfigVosMap.put(dbConfigVO.getDbType(), dbConfigVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建数据库配置的面板
	 */
	public Composite getComposite(Composite parent) {
		Composite dataBaseComposite = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		dataBaseComposite.setLayout(gridLayout);
		
		Label label = new Label(dataBaseComposite, SWT.NONE);
		label.setText("Data Base");
		dataBaseType = new Combo(dataBaseComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
		for (String dataBaseTypeString : dbConfigVosMap.keySet()) {
			dataBaseType.add(dataBaseTypeString);
		}
		dataBaseType.addSelectionListener(new comboListener());
		
		GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
		textGridData.heightHint = 20;
		
		Label dialectLabel = new Label(dataBaseComposite, SWT.NONE);
		dialectLabel.setText("Dialect");
		dialect = new Text(dataBaseComposite, SWT.BORDER);
		dialect.setLayoutData(textGridData);
		
		Label jdbcDriverLabel = new Label(dataBaseComposite, SWT.NONE);
		jdbcDriverLabel.setText("JDBC Driver");
		jdbcDriver = new Text(dataBaseComposite, SWT.BORDER);
		jdbcDriver.setLayoutData(textGridData);

		Label connectionUrlLabel = new Label(dataBaseComposite, SWT.NONE);
		connectionUrlLabel.setText("Connection URL");
		connectionURL = new Text(dataBaseComposite, SWT.BORDER);
		connectionURL.setLayoutData(textGridData);
		
		Label hbm2ddlLabel = new Label(dataBaseComposite, SWT.NONE);
		hbm2ddlLabel.setText("hbm2ddl");
		hbm2ddl = new Combo(dataBaseComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
		hbm2ddl.add("create");
		hbm2ddl.add("create-drop");
		hbm2ddl.add("update");
		hbm2ddl.add("validate");
		
		Label showSqlLabel = new Label(dataBaseComposite, SWT.NONE);
		showSqlLabel.setText("Show Sql");
		showSql = new Combo(dataBaseComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
		showSql.add("true");
		showSql.add("false");
		
		Label usernameLabel = new Label(dataBaseComposite, SWT.NONE);
		usernameLabel.setText("Username");
		username = new Text(dataBaseComposite, SWT.BORDER);
		username.setLayoutData(textGridData);
		
		Label passwordLabel = new Label(dataBaseComposite, SWT.NONE);
		passwordLabel.setText("Password");
		password = new Text(dataBaseComposite, SWT.BORDER);
		password.setLayoutData(textGridData);
		
		showProjectDbConfiguration(projectDbConfigVO);
		
		return dataBaseComposite;
	}
	
	/**
	 * 根据dbconfigVo参数显示数据库配置信息
	 * @param dbconfigVo
	 */
	private void showProjectDbConfiguration(DBConfigVO dbconfigVo) {
		if (dbconfigVo.getDbType() != null ){
			dataBaseType.setText(dbconfigVo.getDbType());
		}
		if (dbconfigVo.getDialect() != null ){
			dialect.setText(dbconfigVo.getDialect());
		}
		if (dbconfigVo.getJdbcDriver() != null) {
			jdbcDriver.setText(dbconfigVo.getJdbcDriver());
		}
		if (dbconfigVo.getConnectionURL() != null) {
			connectionURL.setText(dbconfigVo.getConnectionURL());
		}
		if (dbconfigVo.getHbm2ddl() != null) {
			hbm2ddl.setText(dbconfigVo.getHbm2ddl());
		}
		if (dbconfigVo.getShowSql() != null) {
			showSql.setText(dbconfigVo.getShowSql());
		}
		if (dbconfigVo.getUsername() != null) {
			username.setText(dbconfigVo.getUsername());
		}
		if (dbconfigVo.getPassword() != null) {
			password.setText(dbconfigVo.getPassword());
		}
	}
	
	/**
	 * 根据所选择的数据库类型显示相应的数据库默认配置信息
	 * @param dbType
	 */
	private void refreshDbConfigurationByDbType(String dbType) {
		DBConfigVO dbConfigVO = dbConfigVosMap.get(dbType);
		
		if (projectDbConfigVO.getDbType().equals(dbType)) {
			showProjectDbConfiguration(projectDbConfigVO);
			return;
		}
		
		showProjectDbConfiguration(dbConfigVO);
		selectedDbConfigVO = dbConfigVO;
	}
	
	/**
	 * 点击选择数据库类型的监听器（目前没使用到，待用）
	 * @author xmfang
	 *
	 */
	private class comboListener implements SelectionListener {
		@Override
		public void widgetDefaultSelected(SelectionEvent arg0) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			refreshDbConfigurationByDbType(dataBaseType.getItem(dataBaseType.getSelectionIndex()));
		}
	}
	
	/**
	 * 获得本面板所配置的数据库配置信息
	 * @return
	 */
	public DBConfigVO getDataBaseConfiguration() {
		projectDbConfigVO.setDialect(dialect.getText());
		projectDbConfigVO.setConnectionURL(connectionURL.getText());
		projectDbConfigVO.setJdbcDriver(jdbcDriver.getText());
		projectDbConfigVO.setHbm2ddl(hbm2ddl.getText());
		projectDbConfigVO.setShowSql(showSql.getText());
		projectDbConfigVO.setUsername(username.getText());
		projectDbConfigVO.setPassword(password.getText());
		
		if (!dataBaseType.getText().equals(projectDbConfigVO.getDbType()) && selectedDbConfigVO != null) {
			projectDbConfigVO.setDbType(dataBaseType.getText());
			projectDbConfigVO.setDbGroupId(selectedDbConfigVO.getDbGroupId());
			projectDbConfigVO.setDbArtifactId(selectedDbConfigVO.getDbArtifactId());
		}
		
		return projectDbConfigVO;
	}
	
}
